package com.example.demo.security;


import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 需新建配置类注册一个指定的加密方式Bean，或在下一步WebSecurityConfg配置类中注册指定
     */
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //通过用户名从数据库获取用户信息
        User userInfo = userMapper.selectByLoginName(username);
        if (userInfo == null){
            throw new UsernameNotFoundException("用户名或密码错误");
        }

        //得到用户角色
        //String role = userInfo.getRole();

        //角色集合
        List<GrantedAuthority> authorities = new ArrayList<>();
        //角色必须以'ROLE_'开头，数据库中没有，则在这里加
       //authorities.add(new SimpleGrantedAuthority("ROLE_"+role));
        userInfo.setAuthorities(authorities);

        return userInfo;
    }
}

